package com.qintian.rainstorm.modules.sever.impl;

import com.qintian.rainstorm.datasourcse.DataSourceNames;
import com.qintian.rainstorm.datasourcse.annnotiaon.DataSource;
import com.qintian.rainstorm.modules.dao.IntegerTimeRainingDao;
import com.qintian.rainstorm.modules.entity.IntegerTimeRainingBo;
import com.qintian.rainstorm.modules.sever.RainMonitorServer;
import com.qintian.rainstorm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 **/
@Service("rainMonitorServer")
public class RainMonitorImpl implements RainMonitorServer {

    @Autowired
    IntegerTimeRainingDao integerTimeRainingDao;

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorLiveStainInfo(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainMonitorLiveStainInfo(bo);
    }

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorLive(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainMonitorLive(bo);
    }

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorLiveBy30(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainMonitorLiveBy30(bo);
    }
    //(3 6 12 24 )
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorHour(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainMonitorHour(bo);
    }

    //(36 48 60 72)
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorHourMore(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainMonitorHourMore(bo);
    }





    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorHourAfter(IntegerTimeRainingBo bo) {
        //分辨 产品类型 08 或 20
        String element=bo.getDataBasesTimeParameter();
        String time=bo.getEndTime();
        if(time!=null && time!=""){
              Map<String,String> map=getTime(element,time);
              bo.setStartTime(map.get("sartime"));
              bo.setEndTime(map.get("endtime"));
        }else{
            String startime=integerTimeRainingDao.selectGetMaxTime("58657");
            Map<String,String> map=getTime(element,startime);
            bo.setStartTime(map.get("sartime"));
            bo.setEndTime(map.get("endtime"));
        }

        return integerTimeRainingDao.selectRainMonitorHourAfter(bo);
    }

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainMonitorInterger(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainMonitorInterger(bo);
    }


    private Map<String,String> getTime(String element,String time){
        //20181211 1515
        Map<String,String> map=new HashMap<>();
        StringBuffer sartime=new StringBuffer();
        if(element.equals("PRE_08")){
            int num =new Integer(time.substring(8,10)).intValue();
            if(num>8){
                sartime.append(time.substring(0,8)+"0800");
            }else{
                Date d = DateUtils.stringToDate(time.substring(0,8),DateUtils.DATE_TIME_PATTERN_6);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(d);
                calendar.add(Calendar.DATE, -1);
                calendar.add(Calendar.HOUR_OF_DAY,8);
                sartime.append(DateUtils.format(calendar.getTime(),DateUtils.DATE_TIME_PATTERN_5));
            }
        }else if(element.equals("PRE_20")){
            int num =new Integer(time.substring(8,10)).intValue();
            if(num>20){
                sartime.append(time.substring(0,8)+"2000");

            }else{
                Date d = DateUtils.stringToDate(time.substring(0,8),DateUtils.DATE_TIME_PATTERN_6);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(d);
                calendar.add(Calendar.DATE, -1);
                calendar.add(Calendar.HOUR_OF_DAY,20);
                sartime.append(DateUtils.format(calendar.getTime(),DateUtils.DATE_TIME_PATTERN_5));
            }
        }
        map.put("sartime",sartime.toString());
        map.put("endtime",time);
        return map;
    }

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainStation24HourTrend(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainStation24HourTrend(bo);
    }

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Map<String, Object>> selectRainStation24HourTrendInteger(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectRainStation24HourTrendInteger(bo);
    }

    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public String selectGetMaxTime24Hour(String stationId) {
        return integerTimeRainingDao.selectGetMaxTime24Hour(stationId);
    }
}
